{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .login_box {
            width: 400px;
            padding: 0px 20px 30px 30px;
            border: 1px solid #aaa;
            margin: 150px auto;
            border-radius: 5px;
            box-shadow: 5px 5px 5px #aaa;
        }
    </style>
    <link rel="stylesheet" href="{% static 'plugins/bootstrap-3.4.1/css/bootstrap.min.css' %}">
</head>
<body>
<div class="head"></div>
<div class="logi clearfix">
    <form method="post" novalidate>
    {% csrf_token %}
        <h3 style="text-align: center">{{ title }}</h3>
        {% for item in form %}
            {% if item.name == "code" %}
                <div class="form-group">
                    <label for="exampleInputEmail1">{{ item.label }} :</label>
                    <div class="clearfix">
                        <div class="col-xs-6" style="padding-left: 0">{{ item }}</div>
                        <div class="col-xs-6"><input id="btn_code" class="btn btn-sm btn-default" type="button"
                                                     value="获取验证码"></div>
                        <span style="color: red;"> {{ item.errors.0 }}</span>
                    </div>
                </div>
            {% else %}
                <div class="form-group">
                    <label for="exampleInputEmail1">{{ item.label }} :</label>
                    {{ item }}
                    <span style="color: red;"> {{ item.errors.0 }}</span>
                </div>
            {% endif %}
        {% endfor %}
        <input style="float: right" type="submit" class="btn btn-success" value="提交">
    </form>
</div>
<script src="{% static 'js/jquery-3.5.1.js ' %}"></script>
<script>
    $(function () {
        getcode();
    })

    function getcode() {
        var time = 60
        $("#btn_code").click(function () {
            console.log("点击了");
            $.ajax({
                url: "/goto/?type=register",
                type: "get",
                dataType: "JSON",
                data: {
                    tel: $("#id_mobile_phone").val()
                },
                success: function (res) {
                    console.log(res)
                    if(res.status){
                    $("#btn_code").attr("disabled", true);
                    $("#btn_code").val('还剩' + time + '秒'); //解决点了按钮之后的延迟
                    var timer = setInterval(function () {
                        if (time == 1) {
                            clearInterval(timer);
                            $("#btn_code").attr("disabled", false);
                            $("#btn_code").val('发送');
                            time = 60;
                        } else {
                            $("#btn_code").val('还剩' + (time-1) + '秒');
                            time--;
                        }
                    }, 1000);
                }
                    else{
                        alert(res.msg)
                    }
                }
            })
        })
    }
</script>
</body>
</html>